﻿using DevExpress.Mvvm;
using DevExpress.Mvvm.POCO;
using DotNetExtensions.AspNet.DevExpress.MVVM.ViewModels;
using Habi.Solver.Common;
using Habi.Solver.Models.Resources;
using Habi.Solver.Services;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Habi.Solver.ViewModels
{
    public class Module
    {
        public string Name { get; set; }
        public string Title { get; set; }
        public string Icon { get; set; }
        public string Category { get; set; }
        public string Type { get; set; }
        public Color BackColor { get; set; }
    }

    public class MainViewModel : DocumentViewModel<Module>
    {
        public IDocumentManagerService FlyoutDocumentManagerService
        {
            get { return this.GetService<IDocumentManagerService>("FlyoutDocumentManagerService"); }
        }

        public IControlOperationService OperationService
        {
            get { return this.GetService<IControlOperationService>(); }
        }
        public const string DashboardView = "DashboardView";
        public const string RandomView = "RandomView";
        public const string ResourceMangeView = "ResourceMangeView";
        public const string ResourceTaskView = "ResourceTaskView";
        public const string UploadResourceView = "UploadResourceView";
        public const string LoginView = "LoginView";
        public const string RegsitView = "RegsitView";
        public const string AboutView = "AboutView";

        public string Title { get; set; } = "Welcome to Dex.Express MVVM";

        public virtual string OSName { get; set; }
        public virtual string OSVersion { get; set; }
        public virtual string Mac { get; set; }
        public virtual string IPAddress { get; set; }
        public virtual string Size { get; set; }

        public Dictionary<string, IDocument> Documents { get; private set; } = new Dictionary<string, IDocument>();


        public MainViewModel()
        {
            Messenger.Default.Register<FlyoutDocumentOption>(this, option =>
            {
                if (option.Command == FlyoutDocumentOption.CloseAll || option.ClosePrevDocuments)
                {
                    foreach (var item in FlyoutDocumentManagerService.Documents) item.Close(true);
                }

                if (option.Command == FlyoutDocumentOption.Reflesh && !string.IsNullOrEmpty(option.View))
                {

                }
                else if (option.Command == FlyoutDocumentOption.New && !string.IsNullOrEmpty(option.View))
                {
                    FlyoutDocumentManagerService.ShowNewEntityDocument(this, option.View);
                }
            });

            OSName = AppEnvironment.Default.OSName;
            OSVersion = AppEnvironment.Default.OSVersion;
            Mac = AppEnvironment.Default.Mac;
            Size = AppEnvironment.Default.DriveSize;
            IPAddress = AppEnvironment.Default.IPAddress;
        }

        public void Info()
        {
            try
            {
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                process.StartInfo.FileName = "https://habi.tv";
                process.StartInfo.Verb = "Open";
                process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
                process.Start();
            }
            catch (System.ComponentModel.Win32Exception) { }
        }
        public void About()
        {
            Messenger.Default.Send(new FlyoutDocumentOption(MainViewModel.AboutView, false));
        }
        public void Quit()
        {
            OperationService.Quit();
        }

        public void Minimize()
        {
            OperationService.Minimize();
        }

        public void Maximize()
        {
            OperationService.Maximize();
        }

        public override List<Module> CreateModules()
        {
            return new List<Module> {
                new Module { Name = "Dashboard", Title="Dashboard", Category= "Home", Icon = "Dashboard", BackColor = Color.FromArgb(0x00, 0x87, 0x9C), Type = DashboardView},
                new Module { Name = "Random", Title="Random", Category= "Random", Icon = "Employees", BackColor = Color.FromArgb(0xCC, 0x6D, 0x00), Type = RandomView},
                new Module { Name = "ResourceView", Title="Resource View", Category= "Cate2", Icon = "Products", BackColor = Color.FromArgb(0x40, 0x40, 0x40), Type = ResourceMangeView},
                new Module { Name = "Test4", Title="Test1", Category= "Cate3", Icon = "Customers", BackColor = Color.FromArgb(0x00, 0x73, 0xC4), Type = DashboardView},
            };
        }

        public void ShowTaskView()
        {
            Messenger.Default.Send(new FlyoutDocumentOption(MainViewModel.ResourceTaskView, false));
        }

        public void ShowLoginView()
        {
            if (!AppEnvironment.Default.User.IsLogin)
            {
                Messenger.Default.Send(new FlyoutDocumentOption(MainViewModel.LoginView, false));
            }
        }
    }
}
